<?php
	require_once("settings_mod.php");
	require_once("account_mod.php");

	class SecurityMod {
		public static function checkLogin() {
			$name = v($_REQUEST, "uname");
			$validation= v($_REQUEST, "vcode");
			if (!isset($name) || !isset($validation)) {
				return null;
			}
			$acc = AlertAccountMod::getAccount($name);

			if (is_null($acc) || $acc->getValidation() != $validation) {
				AlertAccountMod::logFailedLogin($name);
				return null;
			} else
				return $acc;
		}

		public static function login() {
			session_name(AlertSettingsMod::SESSION_NAME);
			session_start();
			if (!isset($_SESSION["alert_account"])) {
				$acc = self::checkLogin();
				if (is_null($acc)) {
					print('Invalid user');
					self::logout();
				}
				session_regenerate_id(true);
				$_SESSION["alert_account"] = $acc;
			}
			session_write_close();
		}

		public static function logout() {
			session_name(AlertSettingsMod::SESSION_NAME);
			session_start();
			$_SESSION = array();
			if (isset($_COOKIE[session_name()])) {
				setcookie(session_name(), "", time() - 60 * 60, "/");
			}
			session_destroy();
			//header("Location: login.php");
			exit();
		}
	}
?>